﻿
package com.android.finaltracer.gps;

import android.content.Context;
import android.location.Location;
import android.util.Log;

import com.android.finaltracer.R;
import com.android.finaltracer.model.PositionInfo;
import com.baidu.mapapi.BMapManager;
import com.baidu.mapapi.LocationListener;
import com.baidu.mapapi.MKEvent;
import com.baidu.mapapi.MKGeneralListener;
import com.baidu.mapapi.MKLocationManager;

/**
 * This class communicating with LocationManager service, getting/update
 * location info.
 * 
 * @author zhuhy1
 */
public class PositionListener {
    private static final String TAG = "PositionListener";

    private Location m_lastLocation = null;
    private PositionCallBack mCallback = null;
    private static Context mContext;
    private boolean mIsStarted = false;

    // map manager
    private BMapManager mBMapMan = null;
    private LocationListener mLocationListener = new LocationListener() {
        @Override
        public void onLocationChanged(Location location) {
            if (location != null) {
                String strLog = String.format("您当前的位置:\r\n" + "纬度:%f\r\n"
                        + "经度:%f", location.getLongitude(),
                        location.getLatitude());
                Log.d(TAG, "onLocationChanged - " + strLog);
                reportPosition(location);
            }
        }
    };

    /**
     * Implement this interface to get the location report
     */
    public interface PositionCallBack {
        void onCurrentPosition(PositionInfo position);
    }

    public PositionListener(Context context, PositionCallBack cb) {
        mContext = context;
        mCallback = cb;
    }

    public void start() {
        if (mIsStarted == true) {
            return;
        }

        mBMapMan = new BMapManager(mContext);
        if (mBMapMan != null) {
            mBMapMan.init(mContext.getString(R.string.api_key_baidu),
                    new MyGeneralListener());
            // 注册Listener
            if (mBMapMan.getLocationManager() != null
                    && mLocationListener != null) {
                mBMapMan.getLocationManager().enableProvider(
                        MKLocationManager.MK_GPS_PROVIDER);
                mBMapMan.getLocationManager().setNoitifyInternal(600, 90);
                mBMapMan.getLocationManager().requestLocationUpdates(
                        mLocationListener);
            }
            mBMapMan.start();
        }

        mIsStarted = true;
        Log.d(TAG, "started");
    }

    public void stop() {
        if (!mIsStarted) {
            return;
        }
        if (mBMapMan != null) {
            // 移除listener
            if (mBMapMan.getLocationManager() != null
                    && mLocationListener != null) {
                mBMapMan.getLocationManager().removeUpdates(mLocationListener);
            }

            mBMapMan.stop();
            mBMapMan.destroy();
            mBMapMan = null;
        }

        mIsStarted = false;
        Log.d(TAG, "stoped");
    }

    // 常用事件监听，用来处理通常的网络错误，授权验证错误等
    static class MyGeneralListener implements MKGeneralListener {
        @Override
        public void onGetNetworkState(int iError) {
            Log.d(TAG, "network error");
        }

        @Override
        public void onGetPermissionState(int iError) {
            if (iError == MKEvent.ERROR_PERMISSION_DENIED) {
                // 授权Key错误:
                Log.d(TAG, "api key error");
            }
        }

    }

    public PositionInfo getLastPosition() {
        if (m_lastLocation != null) {
            return PositionInfo.makePosition(
                    (int) (m_lastLocation.getLatitude() * 1E6),
                    (int) (m_lastLocation.getLongitude() * 1E6));
        }
        return null;
    }

    private void reportPosition(Location location) {
        if (location != null && mCallback != null) {
            m_lastLocation = location;
            PositionInfo position = PositionInfo.makePosition(
                    (int) (location.getLatitude() * 1E6),
                    (int) (location.getLongitude() * 1E6));

            mCallback.onCurrentPosition(position);
        }
    }
}
